﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace PJ.Data
{
    public abstract class ObjectWCFProxyProvider<C,S,D,I> : IDisposable, IObjectProvider<I>
        where I : class, IObjectData
        where D : BaseObjectData, I, new()
        where S : class, IObjectProvider<D>
        where C : ObjectProviderClient<S,D>, new()
    {

        #region Constructor

        protected ObjectWCFProxyProvider(IObjectDataCache<I> cache)
        {
            if (cache == null)
                throw new ArgumentNullException("cache");
            _Cache = cache;
        }
        
        #endregion

        

        #region Interface implementation

        public void Dispose()
        {
            if (m_ClientBase != null)
            {
                ClientBase = null;
            }
        }

        public I GetData(string globalId)
        {
            try
            {
                return Cache.Add(ClientBase.GetData(globalId));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }

        public I Find(string globalId)
        {
            try
            {
                return Cache.Add(ClientBase.Find(globalId));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }

        public I CachedGet(string globalId)
        {
            try
            {
                I cache = Cache.Find(globalId);
                if (cache != null)
                    return cache;
                else
                    return Cache.Add(ClientBase.CachedGet(globalId));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }

        public I CachedFind(string globalId)
        {
            try
            {
                I cache = Cache.Find(globalId);
                if (cache != null)
                    return cache;
                else
                    return Cache.Add(ClientBase.CachedFind(globalId));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }

        public bool Exist(string globalId)
        {
            try
            {
                return ClientBase.Exist(globalId);
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }

        public void Delete(string globalId)
        {
            if (string.IsNullOrEmpty(globalId))
                throw new ArgumentNullException();
            try
            {
                ClientBase.Delete(globalId);
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }

        public I PersistAndGet(I data)
        {
            if (data == null)
                throw new ArgumentNullException("data");
            if (string.IsNullOrEmpty(data.GlobalId))
                throw new ArgumentException("data.GlobalId is null");
            try
            {
                return Cache.Add(ClientBase.PersistAndGet(ToContract(data)));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }

        public void Persist(I data)
        {
            if (data == null)
                throw new ArgumentNullException("data");
            if (string.IsNullOrEmpty(data.GlobalId))
                throw new ArgumentException("data.GlobalId is null");
            try
            {
                ClientBase.Persist(ToContract(data));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }

        public virtual I CreateNewItem()
        {
            return new D();
        }



        #endregion

        #region Interface override

        IObjectData IObjectProvider.GetData(string globalId)
        {
            return GetData(globalId);
        }

        IObjectData IObjectProvider.CachedGet(string globalId)
        {
            return CachedGet(globalId);
        }

        IObjectData IObjectProvider.Find(string globalId)
        {
            return Find(globalId);
        }

        IObjectData IObjectProvider.CachedFind(string globalId)
        {
            return CachedFind(globalId);
        }

        IObjectData IObjectProvider.PersistAndGet(IObjectData data)
        {
            return PersistAndGet((I)data);
        }

        void IObjectProvider.Persist(IObjectData data)
        {
            Persist((I)data);
        }

        IObjectData IObjectProvider.CreateNewItem()
        {
            return CreateNewItem();
        }
        
        #endregion     
                

        #region Protected

        protected D CreateNewItem(IObjectData data)
        {
            return (D)CreateNewItem().Init(data);
        }

        protected D ToContract(IObjectData data)
        {
            if (data == null)
                return null;
            D temp = data as D;
            if (temp == null)
                return CreateNewItem(data);
            else
                return temp;
        }

        protected IObjectDataCache<I> Cache
        {
            get { return _Cache; }
        }

        protected C ClientBase
        {
            get
            {
                if (m_ClientBase != null)
                    if (m_ClientBase.State == CommunicationState.Faulted)
                    {
                        m_ClientBase.Abort();
                        ClientBase = null;
                    }
                if (m_ClientBase == null)
                {
                    m_ClientBase = new C();
                }
                return m_ClientBase;
            }
            set
            {
                if (m_ClientBase != null)
                    m_ClientBase.Dispose();
                m_ClientBase = value;
            }
        }

        #endregion

        #region Field

        private readonly IObjectDataCache<I> _Cache;
        private C m_ClientBase;


        #endregion

    }
}
